//
//  R1Controller.m
//  TheRicher
//
//  Created by 李晓东 on 2018/1/16.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "R1Controller.h"

#import "R1Cell.h"

@interface R1Controller () <DatabaseResultDelegate>

//@property (nonatomic, strong) NetService *netService;
//@property (nonatomic, strong) DatabaseService *databaseService;

@end

@implementation R1Controller

- (instancetype)init{
    if (self = [super init]) {
        self.netService = [NetService share];
    }
    return self;
}

- (void)loadView{
    [super loadView];
    
    self.dataArray = [NSMutableArray array];
    self.databaseService = [DatabaseService share];
    self.netQueryResultBlock = ^(id resultString) {
//        [self.tableView reloadData];
        
    };
    [self.tableView setDataSource:self];
    [self.tableView registerClass:[R1Cell class] forCellReuseIdentifier:@"TableCellId"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.先查询数据库
    self.databaseService.delegate = self;
    self.tableName = ShuangSeQiuTableName;

    [self.databaseService queryTable:ShuangSeQiuTableName withCondition:nil ofConditionName:nil andJudgeFlag:nil andOrderBy:@"issueno" andSortBy:1 andCountOf:RowCount];
    // 2.再查询网络
    // 3.添加监听--监听上拉操作
//    self.netService.delegate = self;

}

- (void)loadData{
    [self.netService loadDataFromIssueno:@"2018004" andID:11 andCallBlock:self.netQueryResultBlock];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    R1Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCellId" forIndexPath:indexPath];
    [cell setValues:self.dataArray[indexPath.row]];
    [cell setSelectedBackgroundView:[[UIView alloc]initWithFrame:cell.bounds ]];
    [cell.selectedBackgroundView setBackgroundColor:[UIColor redColor]];
    
    return cell;
}

- (void)resultInDatabase:(NSArray *)resultArray {
    [[KillNumberTools share] type1:resultArray];
    if (resultArray.count == 0) {
        return;
    }
    if ([self.dataArray count] > 0) {
        if ([[(ShuangSeQiu *)self.dataArray.lastObject number] containsString:[resultArray[0] killingNumber]]) {
            [(ShuangSeQiu *)self.dataArray.lastObject setDeadthNumber:[resultArray[0] killingNumber]];
        }
    }

    for (int i = 0; i < resultArray.count - 1; i++ ) {
        if ([[(ShuangSeQiu *)resultArray[i] number] containsString:[(ShuangSeQiu *)resultArray[i + 1] killingNumber]] ) {
            [(ShuangSeQiu *)resultArray[i] setDeadthNumber:[(ShuangSeQiu *)resultArray[i + 1] killingNumber]];
        }
    }
    [self.dataArray addObjectsFromArray:resultArray];
    [self.tableView reloadData];
    
    if ([self.activityIndicatorView isAnimating]) {
        [self.activityIndicatorView stopAnimating];
    }
}

- (void)typeMethodInKillNumberTools:(NSArray *)resultArray{
    [[KillNumberTools share] type1:resultArray];
    
}

- (void)judgeKillingNumber:(NSInteger)resultCount{
    for (int i = 0; i < resultCount + 1; i++) {
        if ([(ShuangSeQiu *)self.dataArray[i] judgeWhetherItContain:[(ShuangSeQiu *)self.dataArray[i + 1] killingNumber
                                                                     ]]) {
            [(ShuangSeQiu *)self.dataArray[i] setDeadthNumber:[(ShuangSeQiu *)self.dataArray[i + 1] killingNumber]];
        }

    }
    self.needToRefreshCount = 0;
    [self.tableView reloadData];
}

- (void)refreshChildControllerView:(NSInteger)refreshCount{
    [self judgeKillingNumber:refreshCount];
}

- (void)resultNeedToInsertKillingDatabase:(NSArray *)insertArray{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[DatabaseService share] insertTableOfKillingNumberWithObjects:insertArray inColumn:@"type1"];

    });
}
@end

